package cn.gdcp.listviewtestbaseadapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

import static android.widget.AdapterView.*;

public class MainActivity extends AppCompatActivity  implements IOnDelListener{

    ArrayList<Student> studentArrayList = new ArrayList<>();
    ArrayList<Student> searchForResult = new ArrayList<>();
      StuAdapter adapter;
      EditText searchContent;
      Button searchBtn;
    ListView listView;
     private int position;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        initStudent();
        initAdapter();
        initListView();
        initAddBtn();
        initSearchView();
        initEdit();

    }

    /**
     * 单击编辑
     */
    private void initEdit() {
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(MainActivity.this,EditActivity.class);
                      MainActivity.this.position=position;
                       startActivityForResult(intent,1);

            }
        });
    }


    /**
     * 初始化搜索模块
     */
    private void initSearchView() {
        searchContent=findViewById(R.id.search_content);
        searchBtn=(Button)findViewById(R.id.search_button);

        searchBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
           String   keyword=searchContent.getText().toString().trim();
                search(studentArrayList,keyword);
                adapter.changeData(searchForResult);
            }
        });
            searchContent.addTextChangedListener(new TextWatcher() {
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }


                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    String   keyword=searchContent.getText().toString().trim();
                    search(studentArrayList,keyword);
                    adapter.changeData(searchForResult);

                }


                public void afterTextChanged(Editable s) {

                }
            });

    }

    private void search(ArrayList<Student> studentArrayList, String keyword) {
               searchForResult.clear();
           for (int i=0;i<studentArrayList.size();i++){
              Student student=studentArrayList.get(i);
               if (student.getName().contains(keyword)){
                   searchForResult.add(student);
               }else if (String.valueOf(student.age).equals(keyword)){
                   searchForResult.add(student);
               }

           }


    }


    /**
     * 初始化添加按钮
     */
    private void initAddBtn() {
        Button addBtn = (Button) findViewById(R.id.add_button);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAddAction();
            }
        });
    }

    /**
     * 初始化学生列表
     */
    private void initListView() {
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }

    /**
     * 初始化适配器
     */
    private void initAdapter() {
        adapter = new StuAdapter(MainActivity.this, studentArrayList,MainActivity.this);
    }

    private void initStudent() {
        for (int i = 0; i < 2; i++) {
            Student student1 = new Student("陆及", 15, R.drawable.m1);
            studentArrayList.add(student1);
            Student student2 = new Student("陆判", 17, R.drawable.m1);
            studentArrayList.add(student2);
            Student student3 = new Student("陆秀夫", 16, R.drawable.m1);
            studentArrayList.add(student3);
            Student student4 = new Student("陆游", 18, R.drawable.m1);
            studentArrayList.add(student4);
            Student student5 = new Student("陆经东", 19, R.drawable.m1);
            studentArrayList.add(student5);
        }


    }


        /*接收intent返回的参数*/
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==101&&resultCode==202){
           String name= data.getStringExtra("NAME");
          int age=Integer.parseInt(data.getStringExtra("AGE"));
          int imgId=data.getIntExtra("IMGID",0);
            Student student=new Student(name,age,imgId);
           studentArrayList.add(student);
           adapter.notifyDataSetChanged();
        }
        else if(requestCode==1&&resultCode==2){
            String name= data.getStringExtra("NAME");
            int age=Integer.parseInt(data.getStringExtra("AGE"));
            int imgId=data.getIntExtra("IMGID",0);
               Student  student=new Student(name,age,imgId);
              studentArrayList.set(position,student);
              adapter.notifyDataSetChanged();


        }
    }

    /* 进入添加界面*/
    private void startAddAction() {
        Intent intent = new Intent(this, AddStudent.class);
        startActivityForResult(intent, 101);
    }

    @Override
    public void del(int i) {
        studentArrayList.remove(i);
        adapter.notifyDataSetChanged();
    }
}
